package com.wwtech.epp.ui.activity.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.entity.Approve;
import com.wwtech.epp.ui.activity.base.MyBaseFragment;
import com.wwtech.epp.ui.activity.work.component.DaggerApproveListComponent;
import com.wwtech.epp.ui.activity.work.contract.ApproveListContract;
import com.wwtech.epp.ui.activity.work.module.ApproveListModule;
import com.wwtech.epp.ui.activity.work.presenter.ApproveListPresenter;
import com.wwtech.epp.ui.adapter.work.ApproveListAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/09/16 23:03:27
 */

public class ApproveListFragment extends MyBaseFragment implements ApproveListContract.View ,SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.RequestLoadMoreListener {

    @Inject
    ApproveListPresenter mPresenter;
    @Inject
    ApproveListAdapter approveLisAdapter;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private String mType;
    private int mNextPage;
    private static final int ONE_PAGE_SIZE = 5;
    private int total;
    private static final String ARG_TYPE = "arg_type";

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_approve_list, null);
       ButterKnife.bind(this, view);
       Bundle mBundle = getArguments();
       refreshLayout.setOnRefreshListener(this);
       approveLisAdapter.setOnLoadMoreListener(this, recyclerView);
       approveLisAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
           @Override
           public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
               Intent intent = new Intent(getActivity(), ApproveDetailActivity.class);
               intent.putExtra("databean", approveLisAdapter.getData().get(i));
               intent.putExtra("type", mType);
               startActivity(intent);
           }
        });
       recyclerView.setAdapter(approveLisAdapter);
       return view;
   }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (getArguments() != null) {
            mType = getArguments().getString(ARG_TYPE);
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFinishProject(String content) {
        if (content.equals("deletApprove") || content.equals("opetateApprove")) {
            fetchData();
        }
    }

    @Override
    public void fetchData() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                loadDataFromServer(true);
            }
        });
    }

    public void loadDataFromServer(boolean showRefresh) {
        refreshLayout.setRefreshing(showRefresh);
        if (showRefresh) {
            mNextPage = 1;
        }
        mPresenter.getApproveListFromServer(mType, mNextPage, ONE_PAGE_SIZE);
    }

    public static ApproveListFragment newInstance(String param) {
        ApproveListFragment fragment = new ApproveListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TYPE, param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onRefresh() {
        KLog.i(mType);
        mNextPage = 1;
        loadDataFromServer(true);
    }

    @Override
    public void onLoadMoreRequested() {
        if (approveLisAdapter.getData().size() < ONE_PAGE_SIZE || approveLisAdapter.getData().size() >= total) {
            approveLisAdapter.loadMoreEnd(true);
            return;
        }
        mNextPage ++;
        mPresenter.getApproveListFromServer(mType, mNextPage, ONE_PAGE_SIZE);
    }


    @Override
    protected void setupFragmentComponent() {
       DaggerApproveListComponent
               .builder()
               .appComponent(((AppConfig) getActivity().getApplication()).getApplicationComponent())
               .approveListModule(new ApproveListModule(this))
               .build()
               .inject(this);
    }
    @Override
    public void setPresenter(ApproveListContract.ApproveListContractPresenter presenter) {
        mPresenter = (ApproveListPresenter) presenter;
    }

    @Override
    protected void initDataFromLocal() {

    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void setListData(Approve approve) {
        KLog.i("设置列表");
        refreshLayout.setRefreshing(false);
        if (mNextPage == 1) {
            total = approve.getData().getTotal();
            approveLisAdapter.setNewData(approve.getData().getList());
        } else {
            approveLisAdapter.loadMoreComplete();
            approveLisAdapter.addData(approve.getData().getList());
        }
    }

}